home *** CD-ROM | disk | FTP | other *** search
- { INDEX.PAS in Turbo Pascal 3.0 for IBM PC and compatibles }
- { A book indexing program -- requires an input file -- execute as .COM }
- { Also requires a Boring Dictionary, BORING.DIC, in the .COM file's directory }
- { To execute .COM file, enter "index <inputfilename> <outputfilename> }
- program INDEX;
- const CR = #13; { carriage return character }
- const maxDict = 3750; {maximum allowable dictionary entries}
- type letters = 'a'..'z';
- wordtype = string[16];
- nodeptr = ^nodetype;
- nodetype = record
- info: wordtype;
- next: nodeptr
- end;
- var inputFile,outputFile: text;
- inputFilename, outputFilename: string[127];
- chr,firstletter: char;
- sortList: array[letters] of nodeptr; { the array of 26 lists }
- i: letters;
- word: wordtype;
- boringWords: array [1..maxDict] of wordtype;
- dictionary : text;
- endDict : integer;
- procedure InitFiles;
- begin { open input and output files }
- inputFilename := paramSTR(1);
- Assign(inputFile,inputFilename);
- Reset(inputFile);
- outputFilename := paramSTR(2);
- Assign(outputFile, outputFilename);
- Rewrite(outputFile);
- end;
- procedure GetWord(VAR infile: text; VAR word: wordtype);
- begin { read a cleaned-up word from the input file }
- word := ''; { initialize to blank }
- repeat
- read(infile,chr);
- if chr in ['A'..'Z'] { convert all to lowercase }
- then chr := char(ord(chr)+32);
- if chr in ['a'..'z'] { only accept alpha characters }
- then word := word+chr; { add to word being built }
- until (chr = ' ') or (chr = CR) or eof(infile)
- end;
- procedure Place(VAR list: nodeptr; word: wordtype);
- var p,q,newnode: nodeptr;
- found: boolean;
- begin { insert new word into list in sorted position only if unique }
- q := nil;
- p := list; { p points to head of list }
- found := false;
- while (p <> nil) { not end of list and }
- and (not found) { word not already here and }
- and (word >= p^.info) do { word alphabetically later than current }
- if p^.info = word { does this node contain our word? }
- then found := true { yes! word is already here }
- else begin
- q := p; { remember this node and }
- p := p^.next { move on to the next one }
- end; {while}
- if not found { word isn't already here }
- then begin
- New(newnode); { create a new node }
- newnode^.info := word; { put word in its info field }
- if q = nil { list was empty }
- then begin
- newnode^.next := list; { newnode becomes first }
- list := newnode
- end
- else begin
- newnode^.next := q^.next; { insert after node q }
- q^.next := newnode
- end
- end
- end;
- procedure SquirtOut(list: nodeptr; VAR outfile: text);
- begin { send sorted list to output file }
- while list <> nil
- begin
- writeln(outfile,list^.info);
- list := list^.next
- end
- end;
- procedure ReadDictionary;
- var i:integer;
- begin
- Assign(dictionary,'BORING.DIC');
- Reset(dictionary);
- i := 1;
- repeat
- readln(dictionary,boringWords[i]);
- i := i + 1
- until eof(dictionary) or (i > maxDict);
- endDict := i; {number of actual dictionary entries}
- Close(dictionary)
- end;
- function Boring(word: wordtype): boolean;
- var left,right,try,svleft,svright: integer;
- begin
- left := 1;
- right := endDict;
- repeat
- svleft := left; svright := right;
- try := (left + right) div 2;
- if word < boringWords[try]
- then right := try - 1
- else left := try + 1;
- until (word = boringWords[try]) or (svleft > svright) ;
- if word = boringWords[try]
- then Boring := true
- else Boring := false
- end;
- begin { main program }
- InitFiles;
- ReadDictionary;
- for i := 'a' to 'z' do sortList[i] := nil; { initialize all the lists }
- while not eof(inputFile) do
- begin
- GetWord(inputFile,word);
- firstletter := word[1]; { get first letter }
- if not Boring(word)
- then Place(sortList[firstletter],word); { put word in proper place }
- end; {while}
- for i := 'a' to 'z' do SquirtOut(sortList[i],outputFile);
- writeln('Keywords are contained in ',outputFilename);
- Close(inputFile);
- Close(outputFile)
- end.
- List[i],outputFile);
- wri